package com.zbkj.admin.controller.platform;

import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.traceability.BatchesFilterRequest;
import com.zbkj.common.response.traceability.BatchesInfoResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.service.service.BatchesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 管理端批次二维码下载控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: xiaolic
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/batches")
@Api(tags = "商品溯源-批次二维码下载")
public class BatchesQrCodeController {

    @Autowired
    private BatchesService batchesService;

    /**
     * 批量下载批次二维码
     * @param batchId 批次ID
     * @param response HTTP响应
     */
    @ApiOperation(value = "批量下载批次二维码")
    @ApiImplicitParam(name = "batchId", value = "批次ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/download/qrcode/{batchId}")
    public void downloadBatchQrCodes(@PathVariable Long batchId, HttpServletResponse response) {
        try {
            batchesService.downloadBatchQrCodes(batchId, response);
        } catch (Exception e) {
            log.error("下载批次二维码失败: {}", e.getMessage(), e);
            try {
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("下载失败：" + e.getMessage());
            } catch (IOException ex) {
                log.error("设置错误响应失败", ex);
            }
        }
    }
    
    /**
     * 获取当前登录用户的所有批次列表
     * @param request 查询条件
     * @param pageParamRequest 分页参数
     * @return 批次列表分页结果
     */
    @ApiOperation(value = "获取当前用户相关批次列表")
    @GetMapping("/list")
    public CommonResult<PageInfo<BatchesInfoResponse>> getBatchesList(
            @Validated BatchesFilterRequest request,
            @Validated PageParamRequest pageParamRequest) {
        try {
            // 获取当前登录用户信息
            SystemAdmin currentAdmin = SecurityUtil.getLoginUserVo().getUser();
            if (currentAdmin == null) {
                return CommonResult.failed("获取当前用户信息失败");
            }
            
            // 调用服务查询批次列表
            PageInfo<BatchesInfoResponse> pageInfo = batchesService.getCurrentAdminBatches(currentAdmin, request, pageParamRequest);
            return CommonResult.success(pageInfo);
        } catch (Exception e) {
            log.error("获取批次列表失败: {}", e.getMessage(), e);
            return CommonResult.failed("获取批次列表失败：" + e.getMessage());
        }
    }
}
